﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace CSBackup.UI
{
    public partial class frmBkpSvc : Form
    {
        public frmBkpSvc()
        {
            InitializeComponent();

            this.listView.Columns.Add("工程名", 60, HorizontalAlignment.Right);
            this.listView.Columns.Add("工程文件路径", 150, HorizontalAlignment.Left);
            this.listView.Columns.Add("备份间隔", 60, HorizontalAlignment.Left);
            this.listView.Columns.Add("开始时间", 140, HorizontalAlignment.Left);
            this.listView.Columns.Add("状态", 60, HorizontalAlignment.Left);
            this.listView.GridLines = false;
            this.listView.FullRowSelect = true;
            this.listView.View = View.Details;
            this.listView.Scrollable = true;
            this.listView.MultiSelect = false;
            this.listView.HeaderStyle = ColumnHeaderStyle.Nonclickable;

        }

        private void frmBkpSvc_Load(object sender, EventArgs e)
        {
            this.RefreshGridView();
        }//frmBkpSvc_Load

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //frmAutoBkp autoDialog = new frmAutoBkp(
            frmAutoBkp autoDialog = new frmAutoBkp();
            autoDialog.ShowDialog(this);
            this.RefreshGridView();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey("software").OpenSubKey("YunGe", true);
            string name = this.listView.Items[listView.SelectedIndices[0]].Text;
            key.DeleteSubKey(name);
            this.RefreshGridView();
        }

        private void RefreshGridView()
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey("software").OpenSubKey("YunGe", true);
            if (key == null)
            {
                key = Registry.LocalMachine.OpenSubKey("software", true).CreateSubKey("YunGe");
            }
            string[] keyNames = key.GetSubKeyNames();

            this.listView.Items.Clear();

            foreach (string sub in keyNames)
            {
                RegistryKey subKey = key.OpenSubKey(sub, true);
                string intervalStr = subKey.GetValue("interval") as string;
                string startTimeStr = subKey.GetValue("startTime") as string;
                string endTimeStr = subKey.GetValue("endTime") as string;
                string repeatStr = subKey.GetValue("repeatCount") as string;
                string lastStr = subKey.GetValue("lastBackup") as string;
                string prjPath = subKey.GetValue("prjPath") as string;

                int intervalInt = Int32.Parse(intervalStr);
                if (intervalInt > 1440)
                    intervalStr = intervalInt / 1440 + "天";
                else if(intervalInt > 60)
                    intervalStr = intervalInt / 60 + "小时";
                else
                    intervalStr = intervalInt + "分钟";

                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = sub;
                item.SubItems.Add(prjPath);
                item.SubItems.Add(intervalStr);
                item.SubItems.Add(startTimeStr);
                if ((repeatStr != null)&&(repeatStr != "0"))
                    item.SubItems.Add("进行中");
                else
                    item.SubItems.Add("完成");

                this.listView.Items.Add(item);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.RefreshGridView();
        }
    }
}
